#!/bin/sh

function printHelp(){

	
	printf "IGE TestSuite\n"
	printf "\n"
	printf "Arguments:\n"
	printf "\n"
	ARG='
    | |--testsuite-location [path]|TestSuite location.[$HOME/IGE-testsuite]
    | |--output-folder [path]|Location where the output files will be stored. [TestSuiteLocation/Results]
    | |--no-testsuite-gsi|Do not generate the testsuite authority infrastructre (CA, usercerts etc.).
    | |--global-test-host [hostname]|Host to test for all services. Exceptions can be specified per component (see below).
    | |--globus-bin-dir [path]|Location of the globus executables. [in $PATH]
    | |--globus-sbin-dir [path]|Location of the superuser globus executables. [in $PATH]
    | |--gsi-alt-username [username]|Alternative username mapped to the test user DN. If not specified, related tests will be skipped. [None]
    | |--setup-service [service1,...]|Comma separated list of services to be installed by the testsuite. [None]
    | |--gsissh-host [hostname]|Hostname to use for gsissh tests. [global-test-host]
    |-h,|--help|This message'

	echo "$ARG" | column -x -t -s "|" -c 80
	printf "\n"
}

CONFIG_FILE=TestSuite.config
if [ `uname` = "Darwin" ] ; then
	alias sed=gsed
fi
TEMP=`getopt -o 'h' --long help,testsuite-location:,output-folder:,no-testsuite-gsi,global-test-host:,gsissh-host:,globus-bin-dir:,gsi-alt-username:,setup-service: -n "$0" -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

#Variables that MUST be set for the testsuite to work
GLOBUS_BIN_DIR=''
USE_TESTSUITE_GSI='yes'
TEST_SUITE_LOCATION="$HOME/IGE-testsuite"
TS_ALT_USERNAME=""
TEST_SERVER=""


while true ; do
	case "$1" in
		--testsuite-location) 
			sed -i "s/TestSuiteLocation=.*/TestSuiteLocation=\"$2\"/" $CONFIG_FILE;
			shift 2;;
		--output-folder)
			sed -i "s/OutputFolder=.*/OutputFolder=\"$2\"/" $CONFIG_FILE;
			shift 2;;
		--no-testsuite-gsi) 
			USE_TESTSUITE_GSI='no'
			sed -i "s/UseTestSuiteGSI=.*/UseTestSuiteGSI=\"no\"/" $CONFIG_FILE;
			shift ;;
		--global-test-host) 
			TEST_SERVER=$2
			sed -i "s/GlobalTestHost=.*/GlobalTestHost=\"$2\"/" $CONFIG_FILE;
			shift 2;;
		--globus-bin-dir)
			GLOBUS_BIN_DIR="$2"
			sed -i "s/TS_GSISSH_BIN=.*/TS_GSISSH_BIN=\"$2/gsissh\"/" $CONFIG_FILE;
			sed -i "s/TS_GSISCP_BIN=.*/TS_GSISCP_BIN=\"$2/gsiscp\"/" $CONFIG_FILE;
			sed -i "s/TS_GLOBUSRUN=.*/TS_GLOBUSRUN=\"$2/globusrun\"/" $CONFIG_FILE;
			sed -i "s/TS_GLOBUSJOBRUN=.*/TS_GLOBUSJOBRUN=\"$2/globus-job-run\"/" $CONFIG_FILE;
			sed -i "s/TS_GLOBUSJOBSTATUS=.*/TS_GLOBUSJOBSTATUS=\"$2/globus-job-status\"/" $CONFIG_FILE;
			sed -i "s/TS_GLOBUSJOBGETOUTPUT=.*/TS_GLOBUSJOBGETOUTPUT=\"$2/globus-job-get-output\"/" $CONFIG_FILE;
			sed -i "s/TS_GLOBUSURLCOPY=.*/TS_GLOBUSURLCOPY=\"$2/globus-url-copy\"/" $CONFIG_FILE;
			shift 2;;
		--gsi-alt-username)
			TS_ALT_USERNAME="$2"
			sed -i "s/TS_ALT_USERNAME=.*/TS_ALT_USERNAME=\"$2\"/" $CONFIG_FILE;
			shift 2;;
		--setup-service)
			SETUP_SERVICES="$2"
			sed -i "s/SetupServices=.*/SetupServices=\"$2\"/" $CONFIG_FILE;
			shift 2;;
		-h|--help)
			printHelp
			shift 
			exit 0
			;;
		--) shift ; break ;;
		*) echo "Internal error!" ; exit 1 ;;
        esac
done

if [ "$GLOBUS_BIN_DIR" = "" ] ; then
	if which gsissh > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GSISSH_BIN=.*|TS_GSISSH_BIN=\"`which gsissh 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GSISSH_BIN=.*|TS_GSISSH_BIN=\"gsissh\"|" $CONFIG_FILE;
	fi
	if which gsiscp > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GSISCP_BIN=.*|TS_GSISCP_BIN=\"`which gsiscp 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GSISCP_BIN=.*|TS_GSISCP_BIN=\"gsiscp\"|" $CONFIG_FILE;
	fi

	if which globusrun > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GLOBUSRUN=.*|TS_GLOBUSRUN=\"`which globusrun 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GLOBUSRUN=.*|TS_GLOBUSRUN=\"globusrun\"|" $CONFIG_FILE;
	fi

	if which globus-job-run > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GLOBUSJOBRUN=.*|TS_GLOBUSJOBRUN=\"`which globus-job-run 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GLOBUSJOBRUN=.*|TS_GLOBUSJOBRUN=\"globus-job-run\"|" $CONFIG_FILE;
	fi

	if which globus-job-status > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GLOBUSJOBSTATUS=.*|TS_GLOBUSJOBSTATUS=\"`which globus-job-status 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GLOBUSJOBSTATUS=.*|TS_GLOBUSJOBSTATUS=\"globus-job-status\"|" $CONFIG_FILE;
	fi

	if which globus-job-get-output > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GLOBUSJOBGETOUTPUT=.*|TS_GLOBUSJOBGETOUTPUT=\"`which globus-job-get-output 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GLOBUSJOBGETOUTPUT=.*|TS_GLOBUSJOBGETOUTPUT=\"globus-job-get-output\"|" $CONFIG_FILE;
	fi

	if which globus-url-copy > /dev/null 2> /dev/null ; then
		sed -i "s|TS_GLOBUSURLCOPY=.*|TS_GLOBUSURLCOPY=\"`which globus-url-copy 2> /dev/null`\"|" $CONFIG_FILE;
	else
		sed -i "s|TS_GLOBUSURLCOPY=.*|TS_GLOBUSURLCOPY=\"globus-url-copy\"|" $CONFIG_FILE;
	fi
fi

if [ "$TEST_SERVER" = "" ]; then
	TEST_SERVER=`hostname -f`
	sed -i "s/GlobalTestHost=.*/GlobalTestHost=\"$TEST_SERVER\"/" $CONFIG_FILE;
fi

echo "The config script is used to setup some basic options in the TestSuite.config file. For more detailed changes please check the config file."
